<template>
  <div>
    <div>
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item title="  点击上传" name="1">
          <el-upload
            class="upload-demo"
            drag
            action="http://10.30.90.45:9991/zuul/api/yun-activiti/deployment/add"
            :before-upload="beforeUpload()"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传zip或bpm</div>
          </el-upload>
        </el-collapse-item>
      </el-collapse>
    </div>
  <hr>
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" size="mini" style="width: 200px" class="filter-item" placeholder="部署"
                v-model="listQuery.name"></el-input>
      <el-button class="filter-item" type="primary" size="mini" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <div style="margin-top: 5px">
      <el-table :key="tableKey" :data="list" v-loading.body="listLoading" size="mini" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="部署ID" min-width="150">
          <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="部署名称" min-width="150">
          <template slot-scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="部署时间" min-width="100" :formatter="dateFormat">
        </el-table-column>
        <el-table-column align="center" fixed="right" min-width="185" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-delete" type="danger"  @click="handelDelete(scope.row)">删除</el-button>
            <el-button size="mini" icon="icon-control-start" type="primary"  @click="handelProcess(scope.row)">启动实例</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading">
        <el-pagination :page-sizes="[5,10,20]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { page, delObj } from '@/api/work/deployment'
import axios from 'axios'
import { getToken } from '@/utils/auth'
export default {
  data () {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      filename: null,
      deploymentManager_btn_edit: false,
      deploymentManager_btn_del: false,
      deploymentManager_btn_add: false,
      listQuery: {
        page: 1,
        limit: 5,
        firstResult: 0,
        maxResults: 5,
        name: undefined
      }
    }
  },
  methods: {
    getList () {
      this.listLoading = true
      page(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
        this.deploymentManager_btn_add = true
      })
    },
    handelDelete (row) {
      delObj(row.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handelProcess (row) {
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.listQuery.maxResults = val - 1
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.listQuery.firstResult = val - 1
      this.getList()
    },
    dateFormat (row, column) {
      if (row === undefined) {
        return ''
      }
      var moment = require('moment')
      return moment(row.deploymentTime).format('YYYY-MM-DD HH:mm:ss')
    },
    beforeUpload (file) {
      let fd = new window.FormData()
      fd.append('key', file, 'fileName')

      // 自己上传文件 想加什么都可以
      axios.post('http://10.30.90.45:9991/zuul/api/yun-activiti/deployment/add', fd, {
        // 加这里
        headers: {
          'access-token': getToken(),
          'Content-Type': 'multipart/form-data'
        }
      })
      return false // 返回false不会自动上传
    }
  },
  created: function () {
    this.getList()
  }
}
</script>
